package com.tywj.buscustomerapp.view.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.ProblemBean;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.utils.myadapter.CommonRcvAdapter;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.item.ProblemItem;
import com.umeng.analytics.MobclickAgent;

import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ProblemActivity extends BaseActivity {
    @BindView(R.id.head_title) TextView title;
    @BindView(R.id.head_left)  ImageView back;
    @BindView(R.id.recycler) RecyclerView recyclerView;
    private CommonRcvAdapter<ProblemBean> adapter;
    private List<ProblemBean> list = new ArrayList<>();
    @Override
    protected int getContentId() {
        return R.layout.activity_problem;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText("常见问题与帮助");
        adapter  = new CommonRcvAdapter<ProblemBean>(list) {
            @NonNull
            @Override
            public AdapterItem createItem(Object type) {
                return new ProblemItem();
            }
        };
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        Observable.create(new ObservableOnSubscribe<List<ProblemBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<ProblemBean>> e){
                try {
                    String msg = WebServicrUtils.getInstance().getProblem();
                    InputStream in_nocode = new ByteArrayInputStream(msg.getBytes("UTF-8"));
                    List<ProblemBean> list = WebServicrUtils.getInstance().getProblems(in_nocode);
                    if (list!=null && list.size()>0)
                        e.onNext(list);
                    else{
                        e.onError(null);
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                } catch (XmlPullParserException e1) {
                    e1.printStackTrace();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ProblemBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(List<ProblemBean> s) {
                        adapter.setData(s);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("问题反馈");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("问题反馈");
        MobclickAgent.onPause(this);
    }
}
